// pages/receiptModify/receiptModify.js
var that = "";
var common = require('../../utils/common.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chargeId: "",
    data: {},
    weIndex: '',
    isM: true,
    total: 0,
    reduceList: [],
    feeItemType: '',
  },
  //水电点击
  WEFeeItemClick: function (e) {
    // var indx = e.target.dataset.index;
    var indx = e.currentTarget.dataset.index
    that.data.weIndex = indx;
    console.log(indx)
    console.log(JSON.stringify(that.data.data.items[indx]))
    wx.navigateTo({
      url: '../WEFeeEditPage/WEFeeEditPage?WEFeeItem=' + JSON.stringify(that.data.data.items[indx]),
    })
  },
  //添加费用点击
  addFeeClick: function () {
    this.setData({
      feeItemType: ''
    })
    wx.navigateTo({
      url: '../fyx/fyx?key=CHARGEITEM',
    })
  },
  //添加减免费用点击
  addReduceFeeClick: function () {
    this.setData({
      feeItemType: 'REDUCE'
    })
    wx.navigateTo({
      url: '../fyx/fyx?key=REDUCE',
    })
  },

  initData: function () {
    common._get('/wx/charge/initUpdateFee', { chargeId: this.data.chargeId },
      //成功
      function (res) {
        console.log(res.data);
        if (res.data.success) {
          //获取数据成功
          res.data.data.items = that.initReduceData(res.data.data.items)
          that.setData({
            data: res.data.data,
          })
          that.sumFun();
        }
      });
  },
  initQuitFee: function (id, time) {
    common._get('/wx/charge/initQuitFee', { roomId: id, endtime: time },
      //成功
      function (res) {
        console.log(res.data);
        if (res.data.success) {
          //获取数据成功
          res.data.data.items = that.initReduceData(res.data.data.items)
          that.setData({
            data: res.data.data,
          })
          that.sumFun();
        }
      });

  },
  //获取减免金额数据
  initReduceData: function (data) {
    console.log(data)
    for (var i = data.length-1;i>=0;i--) {
      if (data[i].reduce==1) {
        data[i].total = data[i].total < 0 ? -data[i].total : data[i].total
        that.data.reduceList.push(data[i]);
        data.splice(i,1)
      }
    }
    that.setData({
      reduceList:that.data.reduceList
    })
    return data;
  },
  //金额输入
  inputChange: function (e) {
    var value = e.detail.value;
    // var indx = e.currentTarget.dataset.index;
    console.log(value)
    this.data.data.housefee = value;
    // this.data.data.items[indx].unitCost = value;
    this.setData({
      data: that.data.data,
    })
    that.sumFun();
  },
  //金额输入
  reduceInputChange: function (e) {
    var value = e.detail.value;
    var indx = e.currentTarget.dataset.index;
    this.data.reduceList[indx].total = value;
    this.data.reduceList[indx].unitCost = value;
    this.setData({
      reduceList: that.data.reduceList,
    })
    that.sumFun();
  },

  //结束时间选择
  endTimeChange: function (e) {
    this.data.data.endDate = e.detail.value
    this.setData({
      data: that.data.data,
    })
  },
  //提交
  submit: function () {
    this.data.data.items = this.data.data.items.concat(this.data.reduceList);
    if (this.data.isM) {
      console.log(this.data.data.items)
      common._get('/wx/charge/saveFee', { chargeId: this.data.chargeId, items: this.data.data.items, endDate: this.data.data.endDate, housefee: 0 },
        //成功
        function (res) {
          app.setRefresh();
          if (res.data.success) {
            wx.showToast({
              title: '保存成功',
            })
            wx.navigateBack({
            })
          }
        });
    } else {
      common._get('/wx/charge/saveQuitFee', { chargeId: this.data.data.chargeId, items: this.data.data.items, endDate: this.data.data.endDate },
        //成功
        function (res) {
          app.setRefresh();
          if (res.data.success) {
            wx.showToast({
              title: '保存成功',
            })
            wx.navigateBack({
            })
          }
        });
    }
    app.setRefresh();
  },
  //删除费用项
  deleteFeeItem: function (e) {
    var that = this;
    var indx = e.currentTarget.dataset.index;
    wx.showModal({
      title: '删除提示！',
      content: '确定要删除' + this.data.data.items[indx].itemName,
      success: function (res) {
        if (res.confirm) {
          that.data.data.items.splice(indx, 1);
          that.setData({
            data: that.data.data
          })
          that.sumFun();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  //删除减免费用项
  reduceDeleteFeeItem: function (e) {
    var that = this;
    var indx = e.currentTarget.dataset.index;
    wx.showModal({
      title: '删除提示！',
      content: '确定要删除' + this.data.reduceList[indx].itemName,
      success: function (res) {
        if (res.confirm) {
          that.data.reduceList.splice(indx, 1);
          that.setData({
            reduceList: that.data.reduceList
          })
          that.sumFun();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    var chargeId = options.chargeId;
    if (chargeId) {
      this.setData({
        chargeId: chargeId
      })
      this.initData();
    }
    var time = options.time;
    var id = options.id;
    if (time && id) {
      this.setData({
        isM: false,
      })
      this.initQuitFee(id, time);
      wx.setNavigationBarTitle({
        title: '退租账单',
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取费用项
    var fee = wx.getStorageSync("feeList");
    wx.removeStorageSync("feeList");
    if (fee) {
      if (this.data.feeItemType == 'REDUCE') {
        //删除相同项减免费用
        fee = app.feeRepeat(fee, this.data.reduceList)
      } else {
        //删除相同项费用
        fee = app.feeRepeat(fee, this.data.data.items)
      }
      for (var i in fee) {

        if (this.data.feeItemType == 'REDUCE') {
          var obj = {
            chargeId: that.data.chargeId,
            chargeType: fee[i].chargeType,
            id: '',
            isforegift: '',
            islowest: '',
            itemKey: fee[i].itemKey,
            itemName: fee[i].itemName,
            lastNum: '',
            lowest: '',
            nowNum: '',
            num: '',
            reduce: '',
            total: '',
            unitCost: '',
            reduce: 1,
            isforgift: 0
          }
          this.data.reduceList.push(obj);
        } else {
          var obj = {
            chargeId: that.data.chargeId,
            chargeType: fee[i].chargeType,
            id: '',
            isforegift: '',
            islowest: '',
            itemKey: fee[i].itemKey,
            itemName: fee[i].itemName,
            lastNum: '',
            lowest: '',
            nowNum: '',
            num: '',
            reduce: '',
            total: '',
            unitCost: '',
          }
          this.data.data.items.push(obj);
        }

      }
      var d = this.data.data;
      that.setData({
        reduceList: that.data.reduceList,
        data: d,
      })
      app.setRefresh();
      that.sumFun();
    }

    //获取水电修改
    var fee = wx.getStorageSync("WEItemChange");
    wx.removeStorageSync("WEItemChange");
    if (fee) {
      this.data.data.items[that.data.weIndex] = fee;
      this.setData({
        data: that.data.data,
      })
      that.sumFun();
      app.setRefresh();
    }
  },
  //计算总额
  sumFun: function () {
    var sum = 0;
    sum += sum + parseFloat(that.data.data.housefee)
    for (var i in that.data.data.items) {
      sum = sum + parseFloat(that.data.data.items[i].total != '' && that.data.data.items[i].total > 0 ? that.data.data.items[i].total:0);
      console.log('sum:' + sum + ' total:' + that.data.data.items[i].total);
    }
    for (var i in that.data.reduceList) {
      sum = sum - parseFloat(that.data.reduceList[i].total != '' && that.data.reduceList[i].total > 0 ? that.data.reduceList[i].total:0)
    }
    that.setData({
      total: sum
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})